package spider.impl.novel;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import spider.NovelSiteEnum;
import spider.entitys.Novel;
import spider.util.NovelSpiderUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 笔下文学网站的书籍列表爬取
 * created by yb
 * on 2017/8/14.
 **/
public class BxwxNovleSpider extends AbstractNovleSpider {


    /**
     * @param url 某个网站的所有小说列表所在的url
     * @return 所有的小说具体信息
     */
    @Override
    public List< Novel > getsNovel( String url, Integer maxTryTimes ) {
        List< Novel > novels = new ArrayList<>();
        try {
            Elements trs = super.getsTr( url,maxTryTimes );
            for ( int i = 1, size = trs.size(); i < size; i++ ) {
                Element tr = trs.get( i );
                Elements tds = tr.getElementsByTag( "td" );
                Novel novel = new Novel();

                novel.setName( tds.first().text() );
                novel.setUrl( tds.first().getElementsByTag( "a" ).first().absUrl( "href" )
                        .replace( ".htm", "/" ).replace( "binfo", "b" ) );
                novel.setLastUpdateChapter( tds.get( 1 ).text() );
                novel.setLastUpdateChapterUrl( tds.get( 1 ).getElementsByTag( "a" ).first().absUrl( "href" ) );
                novel.setAuthor( tds.get( 2 ).text() );
                novel.setLastUpdateTime( NovelSpiderUtil.getDate( tds.get( 4 ).text(), "yy-MM-dd" ) );
                novel.setStatus( NovelSpiderUtil.getNovelStatus( tds.get( 5 ).text() ) );
                novel.setPlatformId( NovelSiteEnum.getNovelSiteEnumByUrl( url ).getId() );

                novels.add( novel );
            }

        } catch ( RuntimeException e ) {
            throw new RuntimeException( e );
        }catch ( Exception e ){
            System.out.println("抓取错误    " + novels.get( novels.size()-1 ));
        }
        return novels;
    }





}
